﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Brain.Build;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Brain.IO;
using Microsoft.Xna.Framework;

namespace Brain.Editor
{
    public partial class PreviewForm : Form
    {
        public ContentBuilder Builder;

        public Texture2D Texture { get; set; }
        public Model Model { get; set; }
        public BrainImage BrainTexture { get; set; }

        private SpriteBatch spriteBatch;
        private ContentManager manager;

        public PreviewForm(ContentProjectFile file)
        {
            InitializeComponent();

            Builder = new ContentBuilder();

            string extension = Path.GetExtension(file.Name).ToLower();
            if (!extension.Contains(".bmodel") || !extension.Contains(".bimg"))
            {
                // We need to import the file Using XNA
                string content = Path.Combine(Engine.Instance.SerializationManager.LevelData.RootFolder, "Content");

                string processor;
                string importer;
                ContentFileUtil.InsertDefaultProcessors(extension, out processor, out importer);


                string name = Path.GetFileNameWithoutExtension(file.Name);
                Builder.Add(file.Name, name, importer, processor, null);
                string error = Builder.Build();
                if (!string.IsNullOrEmpty(error))
                {
                    MessageBox.Show(error);
                }
                else
                {
                    if (extension.Contains("png") || extension.Contains("jpg") ||
                        extension.Contains("tga") || extension.Contains("jpeg"))
                    {
                        manager = new ContentManager(Engine.Instance.ServiceProvider, Builder.OutputDirectory);
                        Texture2D texture = manager.Load<Texture2D>(name);
                        this.Texture = texture;
                        spriteBatch = new SpriteBatch(Engine.Instance.GraphicsDevice);
                    }
                }
            }
            Builder.Dispose();

            this.xnaViewControl1.OnDraw += new SetVoid(xnaViewControl1_OnDraw);
        }

        private void PreviewForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (spriteBatch != null)
            {
                spriteBatch.Dispose();
            }
            if (manager != null)
            {
                manager.Dispose();
            }
            if (Texture != null)
            {
                Texture.Dispose();
            }
        }

        void xnaViewControl1_OnDraw()
        {
            if (Texture != null)
            {
                Engine.Instance.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);
                spriteBatch.Begin();

                Microsoft.Xna.Framework.Rectangle dest = 
                    new Microsoft.Xna.Framework.Rectangle(0, 0, Texture.Width, Texture.Height);
                if (Texture.Width > xnaViewControl1.ClientSize.Width ||
                    Texture.Height > xnaViewControl1.ClientSize.Height)
                {
                    float xRatio = Texture.Width / (float)xnaViewControl1.ClientSize.Width;
                    float yRatio = Texture.Height / (float)xnaViewControl1.ClientSize.Height;
                    float ratio = Math.Max(xRatio, yRatio);// The greatest still
                    dest.Width = (int)(Texture.Width / ratio);
                    dest.Height = (int)(Texture.Height / ratio);
                }
                spriteBatch.Draw(Texture, dest, Microsoft.Xna.Framework.Color.White);
                spriteBatch.End();
            }
        }


    }
}
